package com.cn.kehong.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.ILineTaskService;
import com.cn.kehong.api.IPersonnelManageService;
import com.cn.kehong.api.IProductPackageService;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/lineTask")
public class LineTaskController {

  @Autowired private ILineTaskService iLineTaskService;
  @Autowired
  private IProductPackageService iProductPackageService;
  @Autowired
  private IPersonnelManageService personnelManageService;
  // 获取线边作业列表数据
  @RequestMapping("/getLineTaskList/{frontToken}")
  @ResponseBody
  public JsonResponse<List<LineTask>> getLineTaskList(
      @PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm)
      throws Exception {
    JsonResponse<List<LineTask>> response = new JsonResponse<>();
    List<LineTask> productPackageVos = iLineTaskService.getLineTaskList(queryForm);
    response.setResult(productPackageVos);
    response.setSuccess(true);
    return response;
  }

  // 获取待确认列表数据
  @RequestMapping("/getLineTaskListByTobe/{frontToken}")
  @ResponseBody
  public JsonResponse<List<LineTask>> getLineTaskListByTobe(
          @PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm)
          throws Exception {
    JsonResponse<List<LineTask>> response = new JsonResponse<>();
    List<LineTask> productPackageVos = iLineTaskService.getLineTaskListByTobe(queryForm);
    response.setResult(productPackageVos);
    response.setSuccess(true);
    return response;
  }

  // 品控待确认下载接口
  @ResponseBody
  @RequestMapping("/exportLineTask")
  public void exportLineTask(String queryStr, HttpServletResponse response) throws Exception {
    QueryForm queryForm = new QueryForm();
    if (StringUtils.isNotEmpty(queryStr)) {
      queryStr = URLDecoder.decode(queryStr, "utf-8");
      queryStr = new String(queryStr.getBytes("ISO-8859-1"), "utf-8");
      JSONObject jsonObject = JSON.parseObject(queryStr);
      queryForm.setType(jsonObject.getString("type"));
    }
    iLineTaskService.exportLineTask(queryForm, response);
  }

  // 生产线边任务数量(包含带提交跟已提交的数据)
  @RequestMapping("/getLineTaskListByApp")
  @ResponseBody
  public JsonResponse<Map<String, Object>> getLineTaskListByApp(@RequestBody QueryForm queryForm)
      throws Exception {
    JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
    Map<String, Object> lineTasks = iLineTaskService.getLineTaskListByApp(queryForm);
    response.setResult(lineTasks);
    response.setSuccess(true);
    return response;
  }

  // 除磁当天批号
  @RequestMapping("/getChuciProductCode")
  @ResponseBody
  public JsonResponse<List<ProductChuciPeriod>> getChuciProductCode(
      @RequestBody QueryForm queryForm) throws Exception {
    JsonResponse<List<ProductChuciPeriod>> response = new JsonResponse<>();
    List<ProductChuciPeriod> productChuciPeriods = iLineTaskService.getChuciProductCode(queryForm);
    response.setResult(productChuciPeriods);
    response.setSuccess(true);
    return response;
  }

  // 根据厂房车间任务名称查询
  @RequestMapping("/getNameByNumberList")
  @ResponseBody
  public JsonResponse<List<LineTask>> getNameByNumberList(@RequestBody QueryForm queryForm)
      throws Exception {
    JsonResponse<List<LineTask>> response = new JsonResponse<>();
    List<LineTask> productChuciPeriods = iLineTaskService.getNameByNumberList(queryForm);
    response.setResult(productChuciPeriods);
    response.setSuccess(true);
    return response;
  }

  // 生产线边任务数量
  @RequestMapping("/getLineTaskCountByApp")
  @ResponseBody
  public JsonResponse<Map<String, Object>> getLineTaskCountByApp(@RequestBody QueryForm queryForm) throws Exception {
    JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
    Map<String, Object> lineTaskCountByApp = iLineTaskService.getLineTaskCountByApp(queryForm);
    response.setResult(lineTaskCountByApp);
    response.setSuccess(true);
    return response;
  }

  // 生产线边新增接口
  @RequestMapping("/addApplication")
  @ResponseBody
  public JsonResponse<String> addApplication(@RequestBody LineTask lineTask) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = iLineTaskService.addApplication(lineTask);
    return CommonUtil.getResponseData(response, result);
  }

    // 批次/本班今日或其他修改状态
    @RequestMapping("/updateLineTask")
    @ResponseBody
    public JsonResponse<String> updateLineTask(@RequestBody LineTask lineTask) throws Exception {
        JsonResponse<String> response = new JsonResponse<>();
        String result = iLineTaskService.updateLineTask(lineTask);
        return CommonUtil.getResponseData(response, result);
    }

  //拆机人type="操作工"
  @RequestMapping("/getPersonnelIdAndName")
  @ResponseBody
  public JsonResponse<List<PersonnelManagement>> getPersonnelIdAndName(@RequestBody QueryForm queryForm) throws Exception{
    JsonResponse<List<PersonnelManagement>> response = new JsonResponse<>();
    List<PersonnelManagement> list = personnelManageService.getPersonnelIdAndName(queryForm);
    response.setResult(list);
    response.setSuccess(true);
    return response;
  }

  // 主管品控确认
  @RequestMapping("/updateLineTask1")
  @ResponseBody
  public JsonResponse<String> updateLineTask1(@RequestBody LineTask lineTask) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = iLineTaskService.updateLineTask1(lineTask);
    return CommonUtil.getResponseData(response, result);
  }

  // 包装机维保主管确认
  @RequestMapping("/updateLineTaskByBz")
  @ResponseBody
  public JsonResponse<String> updateLineTaskByBz(@RequestBody LineTask lineTask) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = iLineTaskService.updateLineTaskByBz(lineTask);
    return CommonUtil.getResponseData(response, result);
  }

  // 品控/待确认 MD单包品质  获取单包待确认的数据
  @RequestMapping("/getPackageJobListByTobe/{frontToken}")
  @ResponseBody
  public JsonResponse<List<ProductPackageVo>> getPackageJobListByTobe(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
    JsonResponse<List<ProductPackageVo>> response = new JsonResponse<>();
    List<ProductPackageVo> productPackageVos = iProductPackageService.getPackageJobListByTobe(queryForm);
    response.setResult(productPackageVos);
    response.setSuccess(true);
    return response;
  }

  // 品控待确认MD单包品质下载
  @ResponseBody
  @RequestMapping("/exportPackageJobList")
  public void exportPackageJobList(String queryStr, HttpServletResponse response) throws Exception {
    QueryForm queryForm = new QueryForm();
    if(StringUtils.isNotEmpty(queryStr)){
      queryStr = URLDecoder.decode(queryStr,"utf-8");
      queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
      JSONObject jsonObject = JSON.parseObject(queryStr);
      queryForm.setProductName(jsonObject.getString("productName"));
      queryForm.setProductCode(jsonObject.getString("productCode"));
      queryForm.setFactoryName(jsonObject.getString("factoryName"));
      queryForm.setPackageRoom(jsonObject.getString("packageRoom"));
      queryForm.setLaborTime(jsonObject.getString("labelType"));
      queryForm.setStartTime(jsonObject.getString("startTime"));
      queryForm.setEndTime(jsonObject.getString("endTime"));
    }
    iProductPackageService.exportPackageJobList(queryForm, response);
  }


  //app端 品控/待确认 MD单包品质  获取单包待确认的数据
  @RequestMapping("/getPackageJobListByTobeApp")
  @ResponseBody
  public JsonResponse<Map<String, Object>> getPackageJobListByTobeApp(@RequestBody QueryForm queryForm)
          throws Exception {
    JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
    Map<String, Object> packageJobListByTobeApp = iProductPackageService.getPackageJobListByTobeApp(queryForm);
    response.setResult(packageJobListByTobeApp);
    response.setSuccess(true);
    return response;
  }

  // app中的MD单包品质确认
  @RequestMapping("/updatePackageJobStauts")
  @ResponseBody
  public JsonResponse<String> updatePackageJobStauts(@RequestBody ProductPackageJob productPackageJob) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = iProductPackageService.updatePackageJobStauts(productPackageJob);
    return CommonUtil.getResponseData(response, result);
  }

  //主页线边
  @RequestMapping("/getLineTaskToday/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String,Integer>> getLineTaskToday(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
    JsonResponse<Map<String,Integer>> response = new JsonResponse<Map<String,Integer>>();
    Map<String,Integer>  lineTaskToday = iLineTaskService.getLineTaskToday();
    response.setResult(lineTaskToday);
    response.setSuccess(true);
    return response;
  }


  //线边作业已完成修改
  @RequestMapping("/updateLineTaskByWc/{frontToken}")
  @ResponseBody
  public JsonResponse<String> updateLineTaskByWc(@RequestBody LineTask lineTask, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
    JsonResponse<String> response = new JsonResponse<>();
    String result = iLineTaskService.updateLineTaskByWc(lineTask);
    return CommonUtil.getResponseData(response,result);
  }

  //取消接口
  @RequestMapping("/lineTaskCancel")
  @ResponseBody
  public JsonResponse<String> lineTaskCancel(@RequestBody LineTask lineTask) throws Exception{
    JsonResponse<String> response = new JsonResponse<>();
    String result = iLineTaskService.lineTaskCancel(lineTask.getId());
    return CommonUtil.getResponseData(response,result);
  }

  //线边作业数量(新)
  @RequestMapping("/getLineTaskByNewCount/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String,Object>> getLineTaskByNewCount(QueryForm queryForm, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
    JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
    Map<String,Object>  lineTaskCount = iLineTaskService.getLineTaskByNewCount(queryForm);
    response.setResult(lineTaskCount);
    response.setSuccess(true);
    return response;
  }
  //通过ma mb mc md查下方线边作业数量(新)
  @RequestMapping("/getLineTaskByNewType/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String,Object>> getLineTaskByNewType(QueryForm queryForm, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
    JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
    Map<String,Object>  lineTaskCount = iLineTaskService.getLineTaskByNewType(queryForm);
    response.setResult(lineTaskCount);
    response.setSuccess(true);
    return response;
  }
  //通过ma mb mc md查下方线边作业待确认数量(新)
  @RequestMapping("/getLineTaskByNewTypeQr/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String,Object>> getLineTaskByNewTypeQr(QueryForm queryForm, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
    JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
    Map<String,Object>  lineTaskCount = iLineTaskService.getLineTaskByNewTypeQr(queryForm);
    response.setResult(lineTaskCount);
    response.setSuccess(true);
    return response;
  }

}
